package com.example.sentinel.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.example.sentinel.sentinel.block.handler.FlowBlockHandler;
import com.example.sentinel.sentinel.fall.back.FlowFallBack;
import com.example.sentinel.service.SentinelService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 流控规则
 * blockHandler针对限流问题，fallback针对接口异常问题
 * @author chengjian 
 * @date 2021/6/30
 */
@RestController
public class FlowController {

    @Resource
    private SentinelService sentinelService;

    /**
     * 主要测试blockHandler方法
     * @author chengjian
     * @date 2021/6/30 4:31 下午
     */
    @GetMapping(value = "/limit/stream")
    @SentinelResource(value = "ok",
            blockHandlerClass = FlowBlockHandler.class, blockHandler = "limitStreamBlockHandler",
            fallbackClass = FlowFallBack.class, fallback = "limitStreamFallBack")
    public String limitStream(){
        return sentinelService.limitStream();
    }


    /**
     * 主要测试fallback用法
     * @author chengjian
     * @date 2021/6/30 4:31 下午
     */
    @GetMapping(value = "/limit/stream/v2")
    @SentinelResource(value = "ok",
            blockHandlerClass = FlowBlockHandler.class, blockHandler = "limitStreamBlockHandler",
            fallbackClass = FlowFallBack.class, fallback = "limitStreamFallBack")
    public String limitStreamV2(){
        int a = 1/0;
        return "OK" + a;
    }
}
